package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.Util.QiNiuUtil;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Package;
import com.itheima.service.PackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;


@RestController
@RequestMapping("/package")
public class PackageController {

    @Reference
    private PackageService packageService;

    @Autowired
    private JedisPool jedisPool;

    @GetMapping("/getPackage")
    public Result getPackage(){
        List<Package> list = null;
        Jedis jedis = jedisPool.getResource();
        try {
            String jedis_list = jedis.get(RedisConstant.MOBILE_PACKAGE_LIST);
            if (jedis_list == null) {
                list = packageService.findAll();
                jedis.set(RedisConstant.MOBILE_PACKAGE_LIST, JSON.toJSONString(list));
            } else {
                list = JSON.parseArray(jedis_list, Package.class);
            }

            // 调用业务服务获取套餐列表
            // 拼接图片的链接地址
            list.forEach(pkg->{
                if (pkg.getImg() != null) {
                    String imageUrl = QiNiuUtil.DOMAIN + "/" + pkg.getImg();
                    pkg.setImg(imageUrl);
                } else {
                    //默认图片展示
                    pkg.setImg(QiNiuUtil.DOMAIN + "/" + RedisConstant.PACKAGE_PIC_DEFAULT);
                }
            });
        /*for (Package pkg : list) {
            String imageUrl = QiNiuUtil.DOMAIN + "/" + pkg.getImg();
            pkg.setImg(imageUrl);
        }*/
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    @GetMapping("/findById")
    public Result findById(int id){
        Package pkg = null;
        Jedis jedis = jedisPool.getResource();
        try {
            String pkgDetail = jedis.get(RedisConstant.MOBILE_PACKAGE_DETAIL + id);
            if (pkgDetail == null) {
                pkg = packageService.findById(id);
                if (pkg.getImg() != null) {
                    pkg.setImg(QiNiuUtil.DOMAIN + "/" + pkg.getImg());
                } else {
                    pkg.setImg(QiNiuUtil.DOMAIN + "/" + RedisConstant.PACKAGE_PIC_DEFAULT);
                }
                jedis.set(RedisConstant.MOBILE_PACKAGE_DETAIL + id, JSON.toJSONString(pkg));
            } else {
                pkg = JSONObject.parseObject(pkgDetail, Package.class);
            }
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,pkg);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    @GetMapping("/findPackageById")
    public Result findPackageById(int id){
        try {
            Package pkg = packageService.findPackageById(id);
            pkg.setImg(QiNiuUtil.DOMAIN + "/" + pkg.getImg());
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,pkg);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }



}
